const DB = wx.cloud.database().collection("aid_knowledge")
let ID = ""
let collect = ""

Page({

  /**
   * 页面的初始数据
   */
  data: {
    aid_knowledge: [],
    doubt_desc: [],
    rumor_desc: [],
    suggest: [],
    collect_imgUrl:"https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_knowledge/%E6%94%B6%E8%97%8F%20(3).png?sign=fe4cb6c28ca9600dc9bd6a8f116da511&t=1589163617",
    comment:[], //评论数组,
    content:"",
    username: '',
    hpost_comment:true,

  },
  // 点击发表评论，显示内容
  post_comment: function (e) {
    this.setData({
      hpost_comment: !this.data.hpost_comment,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    ID = options.id;
    console.log("详情页接收的id",ID);
    DB.doc(ID).get().then(res => {
      console.log("详情页成功",res)
      collect = res.data.collect
      console.log(collect)
      this.setData({
        collect_imgUrl:collect ? "https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_knowledge/%E5%B7%B2%E6%94%B6%E8%97%8F3.png?sign=49cbe1f2f81bef2425d97a35f4d4dfae&t=1589164729"
          :"https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_knowledge/%E6%94%B6%E8%97%8F%20(3).png?sign=fe4cb6c28ca9600dc9bd6a8f116da511&t=1589163617",
        comment : res.data.comment,
        })
    })
    .catch(res => {
      console.log("失败", res)
    })
    DB.where({
      _id:ID
    })
    .get()
      .then(res => {
        this.setData({
          aid_knowledge: res.data,
          doubt_desc: res.data[0].doubt_desc.split("|"),
          rumor_desc: res.data[0].rumor_desc.split("|"),
          suggest: res.data[0].suggest.split("|"),
        })

      })
      .catch(res => {
        console.log("失败", res)
      })
  },
    // 收藏点击
    clickCollect(){
      this.setData({
        collect_imgUrl:collect ? "https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_knowledge/%E6%94%B6%E8%97%8F%20(3).png?sign=fe4cb6c28ca9600dc9bd6a8f116da511&t=1589163617"
        :"https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_knowledge/%E5%B7%B2%E6%94%B6%E8%97%8F3.png?sign=49cbe1f2f81bef2425d97a35f4d4dfae&t=1589164729"
      })
      collect = !collect
      wx.cloud.callFunction({
        name:"aid_features",
        data:{
          action:"collect",
          id:ID,
          collect:collect
        }
      }).then(res=>{
        console.log("改变收藏状态成功",res)
      })
      .catch(res=>{
        console.log("改变收藏状态失败",res)
      })
    },
// 获取用户输入的评论内容
getContent(event){
  // console.log("获取评论内容",event);
  this.setData({
    content:event.detail.value
  })
},

// 获取用户头像昵称等信息
onGotUserInfo: function (e) {
  console.log("用户昵称",e);
  this.setData({
    username: e.detail.userInfo.nickName,
  })
},

// 发表评论
publish(){
  let username = this.data.username
  let content = this.data.content
  if(content.length < 4){
    wx.showToast({
      icon:"none",
      title: '评论太短了',
    })
    return
  }
  let commentItem = {}
  commentItem.name = username
  // commentItem.name = "匿名用户"
  commentItem.content = content
  let commentArr = this.data.comment
  commentArr.push(commentItem)
  // console.log("添加后的评论数组",commentArr)
  wx.showLoading({
    title: '发表中···',
  })
  wx.cloud.callFunction({
    name:"aid_features",
    data:{
      action:"publish",
      id:ID,
      comment:commentArr
    }
  }).then(res => {
    console.log("发表评论成功",res)
    this.setData({
      comment:commentArr,
      content:""
    })
    wx.hideLoading({
      complete: (res) => {},
    })
  }).catch(res => {
    console.log("发表评论失败",res)
    wx.hideLoading({
      complete: (res) => {},
    })
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})